package com.example.recycleview;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;

public class MainActivity5 extends AppCompatActivity {
    private Button button8,button9;
    private Intent intent;
    private   MyService2.MyBinder mybinder;
    private  ServiceConnection connection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);
        button8=findViewById(R.id.button8);
        button9=findViewById(R.id.button9);
        button8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity5.this, MyService2.class);
                intent.setType("MainActivity5");
                connection = new ServiceConnection() {
                    @Override
                    public void onServiceConnected(ComponentName componentName, IBinder service) {
                        mybinder = (MyService2.MyBinder) service;
                        mybinder.play();
                    }
                    @Override
                    public void onServiceDisconnected(ComponentName componentName) {

                    }
                };
                bindService(intent, connection, Context.BIND_AUTO_CREATE);
            }
        });
        button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                unbindService(connection);
            }
        });
    }
}